/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.oracle.SimpleSuggestion;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.form.impl.TextBoxItemImpl;
import com.genia.toolbox.web.gwt.form.client.form.impl.VerticalFormImpl;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.genia.toolbox.web.gwt.form.client.value.FormValues;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormSimpleValueImpl;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormValuesImpl;
import com.genia.toolbox.web.gwt.form.server.provider.FormProvider;

public class Form2
    implements FormProvider
{

  private static Form form = null;

  public static final List<FormValues> formValues = new ArrayList<FormValues>();



  public String getDisplayString(final String fieldName, final String value)
  {
    // Nothing to do.
    return null;
  }



  public Form getForm(DataIdentifier dataIdentifier)
  {
    if (form == null) {
      final VerticalFormImpl vForm = new VerticalFormImpl();
      final TextBoxItemImpl tBox = new TextBoxItemImpl();
      tBox.setName("name");
      tBox.setLabelKey(new GwtI18nMessage("Name"));
      vForm.addForm(tBox);
      form = vForm;
    }
    return form;
  }



  public String getFormIdentifier()
  {
    return "form2";
  }



  /*
   * (non-Javadoc)
   * 
   * @see com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getIntialValue(java.lang.String)
   */
  public FormValues getInitialValue(final DataIdentifier dataIdentifier)
  {
    if (dataIdentifier.getIdentifier() == null) {
      FormValuesImpl defaultValues = new FormValuesImpl();
      defaultValues.setFormIdentifier(getFormIdentifier());
      FormSimpleValueImpl fv;
      fv = new FormSimpleValueImpl();
      fv.setValue(null);
      fv.setName("name");
      defaultValues.addFormSimpleValue(fv);
      defaultValues.setDataIdentifier(dataIdentifier);
      return defaultValues;
    }
    final Integer index = Integer.valueOf(dataIdentifier.getIdentifier());
    return formValues.get(index);
  }



  public List<SimpleSuggestion> getSuggestions(DataIdentifier dataIdentifier, final String fieldName, final String query, final int limit)
  {
    // Nothing to do.
    return null;
  }



  public FormValues saveForm(final FormValues newValues)
  {
    if (newValues.getDataIdentifier().getIdentifier() == null) {
      formValues.add(newValues);
      newValues.getDataIdentifier().setIdentifier(formValues.size() - 1);
    }
    else {
      formValues.set(Integer.parseInt(newValues.getDataIdentifier().getIdentifier()), newValues);
    }
    return newValues;
  }

}
